/* 调查催办 dialog */
import React, { Component } from 'react';
import { toJS } from 'mobx';
import { inject, observer } from 'mobx-react';
import { WeaDialog, WeaMoreButton, WeaLocaleProvider, WeaSearchGroup, WeaFormItem } from 'ecCom';
import { WeaSwitch } from 'comsMobx';
import { Button, Spin } from 'antd';

const getLabel = WeaLocaleProvider.getLabel;

@inject('votingVotingList')
@observer
export default class UrgentDialog extends Component {
  static defaultProps = {
    prefixCls: 'voting-urgen'
  }

  renderForm = (form, field, config, renderItem, switchConfig) => {
    const preCls = 'voting-form'
    return (
      <WeaFormItem
        label={`${field.label}`}
        labelCol={{ span: `${field.labelcol}` }}
        wrapperCol={{ span: `${field.fieldcol}` }}
        className={`${preCls}-formItem ${preCls}-${field.domkey[0]}`}
        error={form.getError(field)}
        tipPosition="bottom"
        {...config}
      >
        <WeaSwitch
          fieldConfig={field}
          form={form}
          {...switchConfig}
        />
        { renderItem }
      </WeaFormItem>
    )
  }

  getForm = (form, fields) => {
    const { isFormInit } = form;
    let group = [];
    isFormInit && fields && fields.map((c, index) => {
      let items = [];
      c.items.map(field => items.push({com: this.renderForm(form, field)}))
      group.push(<WeaSearchGroup col={1} title={c.title} needTigger={true} showGroup={c.defaultshow} items={items}/>)
    })
    return group;
  }

  onCancel = () => {
    const { votingVotingList } = this.props;
    const { setStoreState, urgentForm } = votingVotingList;
    setStoreState({urgentVisible: false});
    urgentForm.resetForm();
  }

  render() {
    const { prefixCls, votingVotingList } = this.props;
    const { urgentVisible, urgentLoading, urgentForm, urgentFileds, submitUrgen } = votingVotingList;
    const buttons = [
      <Button type="primary" onClick={() => submitUrgen(this.onCancel)}>{getLabel('725','提交')}</Button>,
      <WeaMoreButton/>
    ]
    return (
      <WeaDialog
        className={prefixCls}
        visible={urgentVisible}
        title={getLabel('17599','网上调查')}
        icon="icon-coms-investigate"
        iconBgcolor="#6D3CF7"
        buttons={buttons}
        onCancel={this.onCancel}
        style={{width: 600}}
      >
        <Spin spinning={urgentLoading}>
          { this.getForm(urgentForm, toJS(urgentFileds)) }
        </Spin>
      </WeaDialog>
    )
  }
}
